﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.sysDAL;
using dhDataAcess.Model;
using dhDataAcess.dataAcess.sysDataSetTableAdapters;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.baseBLL;

namespace dhSystemBLL.basicData
{
   public class itemListBLL
    {
       private itemListTableAdapter  _itemListAdapter = null;
       private itemListTableAdapter  itemListAdapter
        {
            get
            {
                if (_itemListAdapter == null) _itemListAdapter = new itemListTableAdapter ();
                return _itemListAdapter;
            }
        }
        /// <summary>
       /// 查询模块表格
        /// </summary>
        /// <returns></returns>

       public sysDataSet.itemListDataTable  getitemListAll()
        {
            try
            {
                
                return itemListAdapter.GetDataitemListAll();
                
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }

        /// <summary>
       /// 根据模块编号查询表
        /// </summary>
        /// <param name="itemNum"></param>
        /// <returns></returns>

       public sysDataSet.itemListDataTable  getitemListAll(string itemNum)
        {
            try
            {

                return itemListAdapter.GetDataitemByNum(itemNum); 

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }

       public sysDataSet.itemListDataTable getitemListBymoduleNum(string mouduleNum)
       {
           try
           {
               
               return itemListAdapter.GetDataByitemListModuleNum (mouduleNum );

           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }


        /// <summary>
       /// 插入信息
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>

       public int itemListInsert(itemListModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                itemListDAL itemList = new itemListDAL(tn, conn);
                itemList.itemListInsert(row);
                tn.Commit();
                return i;
            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个列编号已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
       /// 更新
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>

       public int itemListUpdate(itemListModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                itemListDAL itemList = new itemListDAL(tn, conn);
                itemList.itemListUpdate(row);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
       /// 删除
        /// </summary>
        /// <param name="itemNum"></param>
        /// <returns></returns>

       public int itemListDel(string itemNum)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                itemListDAL itemList = new itemListDAL(tn, conn);
                itemList.itemListDel(itemNum);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
       /// <summary>
       /// 根据列编号返回列名称
       /// </summary>
       /// <param name="itemNum"></param>
       /// <returns></returns>

       public int itemListRetName(string itemNum)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                itemListDAL itemList = new itemListDAL(tn, conn);
                itemList.itemListretName(itemNum);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }


       public int itemListUpdateALL(sysDataSet.itemListDataTable itemListTable)
       {
           try
           {
               return itemListAdapter.Update(itemListTable);
           }
           catch(Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
    
    }
}
